L04 Maps

Data Visualization (STAT 302)

Author

Olivia Harbison

Overview

The goal of this lab is to explore various ways of building maps with ggplot2.

Challenges are not mandatory for students to complete. We highly recommend students attempt them though. We would expect graduate students to attempt the challenges.

Datasets

We’ll be using the US_income.rda dataset which should be placed in the /data subdirectory in our data_vis_labs project. You’ll also be downloading your own data to build maps.

Code
# Load package(s)
library(tidyverse)
library(sf)
library(tigris)
library(viridis)
library(statebins)


# Load dataset(s)
load("data/US_income.rda")

Exercise 1

Plot 1

Make a county map of a US state using geom_polygon(). Maybe use your home state or a favorite state. Please do NOT use the state in the ggplot2 book example.

Optional: Consider adding major cities (or your home town).

Hints:

  • See section 6.1 in our book.
  • Void theme
Code
pa_counties <- map_data("county", "pennsylvania") %>%
  dplyr::select(long = long, lat, group, id = subregion)

ggplot(pa_counties, aes(long, lat)) +
  geom_polygon(aes(group = group), fill = NA, color = "grey50") +
  coord_quickmap() +
  theme_void() +
  labs(title = "Pennsylvania")

Plot 2

Now use geom_sf() instead. You’ll need to download data for this. You can use either the tigris (github page) or geodata packages. Either tigriscounties() with cb = TRUE or geodata’s gadm() could be useful.

Code
pa_data <- counties("PA", cb = TRUE, progress_bar = FALSE)

ggplot(pa_data) + 
  geom_sf(fill = NA) +
  theme_void() +
  ggtitle("Pennsylvania")

Exercise 2

Using the US_income dataset, recreate the following graphics as precisely as possible.

Code
# Setting income levels
US_income <- mutate(
  US_income,
  income_bins = cut(
    ifelse(is.na(median_income), 25000, median_income),
    breaks = c(0, 40000, 50000, 60000, 70000, 80000),
    labels = c("< $40k", "$40k to $50k", 
               "$50k to $60k", "$60k to $70k", "> $70k"),
    right = FALSE
  )
)

Plot 1

Hints:

  • geom_sf() — boundary color is "grey80" and size is 0.2
  • viridis package (discrete = TRUE in scale_* function)
  • Void theme
Code
ggplot(US_income, aes(fill = income_bins, geometry = geometry)) + 
  geom_sf(color = "grey80", size = 0.2) +
  theme_void() +
  scale_fill_viridis(discrete = TRUE) +
  labs(fill = "Median\nIncome")

Plot 2

Hints:

  • statebins::geom_statebins()
  • viridis package (discrete = TRUE in scale_* function)
  • Statebins theme
Code
ggplot(US_income, aes(state = name, fill = income_bins)) + 
  statebins::geom_statebins() +
  theme_void() +
  scale_fill_viridis(discrete = TRUE) +
  labs(fill = "Median\nIncome")

Exercise 3

Pick any city or foreign country to build a map for. You can dress it up or make it as basic as you want. Also welcome to try building a graphic like that depicted at the end of section 6.5 — use a different region though.

Code
#France
france <- map_data("france", progress_bar = FALSE) %>%
  dplyr::select(long = long, lat, group, id = subregion)

ggplot(france, aes(long, lat)) +
  geom_polygon(aes(group = group), fill = NA, color = "grey50") +
  coord_quickmap() +
  theme_void() +
  labs(title = "France")

Code
philly_roads <- roads("PA", "Philadelphia", progress_bar = FALSE)

ggplot(philly_roads) +
  geom_sf() +
  theme_void()

Code
philly <- tracts("PA", "Philadelphia", progress_bar = FALSE)

ggplot(philly) +
  geom_sf() +
  theme_void()

Challenge(s)

Optional

Using the tidycensus package and few others, try to create a map like below using these directions. Try using a different geographical area and a different variable from the ACS.